<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Python 第一课 | 麦甜怪圈 Xiaomai Circle</title><meta name="author" content="小麦 Andrew Xiaomai"><meta name="copyright" content="小麦 Andrew Xiaomai"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="前言本教程是小麦根据自己的开发经验和研究对python知识点的整理，尝试使用简单易懂的方式让读者入门python这门强大的编程语言。主要参考官方的Python教程 课前须知Python环境安装到Welcome to Python.org下载一个Python。本教程用的是Python 3.8.6版本。 Python 教学【第一课】常用CMD指令dir - directory，显示当前文件夹下的目录列">
<meta property="og:type" content="article">
<meta property="og:title" content="Python 第一课">
<meta property="og:url" content="https://kingsmai.github.io/2021/10/09/%E7%AC%AC%201%20%E8%AF%BE/index.html">
<meta property="og:site_name" content="麦甜怪圈 Xiaomai Circle">
<meta property="og:description" content="前言本教程是小麦根据自己的开发经验和研究对python知识点的整理，尝试使用简单易懂的方式让读者入门python这门强大的编程语言。主要参考官方的Python教程 课前须知Python环境安装到Welcome to Python.org下载一个Python。本教程用的是Python 3.8.6版本。 Python 教学【第一课】常用CMD指令dir - directory，显示当前文件夹下的目录列">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://wallpaper.dog/small/5558009.jpg">
<meta property="article:published_time" content="2021-10-09T13:56:53.000Z">
<meta property="article:modified_time" content="2023-12-17T22:44:35.162Z">
<meta property="article:author" content="小麦 Andrew Xiaomai">
<meta property="article:tag" content="游戏开发, 程序员, 留学生, 中国">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://wallpaper.dog/small/5558009.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kingsmai.github.io/2021/10/09/%E7%AC%AC%201%20%E8%AF%BE/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: {"limitDay":365,"position":"top","messagePrev":"这篇文章已经","messageNext":"天未被更新了，可能里边的内容已经过时，请注意斟酌。"},
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 小麦 Andrew Xiaomai","link":"链接: ","source":"来源: 麦甜怪圈 Xiaomai Circle","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体中文","cht_to_chs":"你已切换为简体中文","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: true,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Python 第一课',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-12-18 06:44:35'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="https://at.alicdn.com/t/c/font_4366094_hx3hbrje2st.css"><meta name="generator" content="Hexo 7.0.0"><link rel="alternate" href="/atom.xml" title="麦甜怪圈 Xiaomai Circle" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="麦甜怪圈 Xiaomai Circle" type="application/rss+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://avatars.githubusercontent.com/u/23566051?s=400&amp;u=cc8834557b9fe7ca3db3adc89606ae47503e603d&amp;v=4" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">37</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 文章</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 列表</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://wallpaper.dog/small/5558009.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="麦甜怪圈 Xiaomai Circle"><img class="site-icon" src="/img/logos/Logo-11.jpg"/><span class="site-name">麦甜怪圈 Xiaomai Circle</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 文章</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 列表</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Python 第一课</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-09T13:56:53.000Z" title="发表于 2021-10-09 21:56:53">2021-10-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-12-17T22:44:35.162Z" title="更新于 2023-12-18 06:44:35">2023-12-18</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/Python/">Python</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>4分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Python 第一课"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>本教程是小麦根据自己的开发经验和研究对python知识点的整理，尝试使用简单易懂的方式让读者入门python这门强大的编程语言。主要参考官方的Python教程</p>
<h1 id="课前须知"><a href="#课前须知" class="headerlink" title="课前须知"></a>课前须知</h1><h2 id="Python环境安装"><a href="#Python环境安装" class="headerlink" title="Python环境安装"></a>Python环境安装</h2><p>到<a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.python.org/">Welcome to Python.org</a>下载一个Python。本教程用的是<code>Python 3.8.6</code>版本。</p>
<h1 id="Python-教学【第一课】"><a href="#Python-教学【第一课】" class="headerlink" title="Python 教学【第一课】"></a>Python 教学【第一课】</h1><h2 id="常用CMD指令"><a href="#常用CMD指令" class="headerlink" title="常用CMD指令"></a>常用CMD指令</h2><p><code>dir</code> - directory，显示当前文件夹下的目录列表</p>
<p><code>cd &lt;文件夹名|完整路径&gt;</code> - change directory，跳转到某个目录<br><code>cd ..</code> 跳转到上一个目录<br><code>cd /</code>跳转到根目录</p>
<p><code>cls</code> - clear screen 清屏</p>
<p><code>&lt;盘符&gt;:</code> - 如<code>D:</code>跳转到该盘下。</p>
<p><code>python &lt;文件名&gt;.py</code> 执行python文件</p>
<h2 id="Python-基础：Hello-World"><a href="#Python-基础：Hello-World" class="headerlink" title="Python 基础：Hello, World!"></a>Python 基础：Hello, World!</h2><h3 id="注释"><a href="#注释" class="headerlink" title="# - 注释"></a><code>#</code> - 注释</h3><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># Python里的单行注释是长这样的</span></span><br><span class="line"><span class="comment"># 注释可以用来取消掉某行代码的执行（被注释的代码将不会被执行）</span></span><br><span class="line"><span class="comment"># 注释是用来阐明代码的，不会被 Python 解释</span></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">Python 里还有另一种长注释，可以通过三个单引号或双引号包含起来</span></span><br><span class="line"><span class="string">这样的话，就可以写多行注释而不需要每一行都添加个井号。</span></span><br><span class="line"><span class="string">事实上这个写法也是个多行字符串，只是如果我们不去对这段字符串进行任何操作，就可以当作多行注释来用</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
<h3 id="输出print-在显示器上"><a href="#输出print-在显示器上" class="headerlink" title="输出print() - 在显示器上"></a>输出<code>print()</code> - 在显示器上</h3><p>能输出的字段包括但不限于字符串（除了以下三个，还能输出其他类型的数据）</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;Hello, world!&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="number">123</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;a&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p><strong>注意：</strong>Python语句结尾不需要 ‘;’，否则会爆错！</p>
<p>可以在不用进行类型转换的情况下输出多个不同类型的数据</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">myName = <span class="string">&quot;Andrew&quot;</span></span><br><span class="line">myAge = <span class="number">20</span></span><br><span class="line"><span class="built_in">print</span>(myName, myAge) <span class="comment"># Andrew 20</span></span><br></pre></td></tr></table></figure>
<h3 id="输入input-用户从键盘输入"><a href="#输入input-用户从键盘输入" class="headerlink" title="输入input() - 用户从键盘输入"></a>输入<code>input()</code> - 用户从键盘输入</h3><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">myName = <span class="built_in">input</span>(<span class="string">&quot;Please input your name: &quot;</span>) <span class="comment"># input的参数（括号里的字符串）是给用户的输入提示</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;My name is: &quot;</span> + myName)             <span class="comment"># &#x27;+&#x27; 号可以用来拼接字符串（后续会详解）</span></span><br></pre></td></tr></table></figure>
<p><code>input()</code>函数的返回值（用户输入的值）一定是字符串，不论用户输入的是任意类型的值</p>
<h3 id="变量及赋值"><a href="#变量及赋值" class="headerlink" title="变量及赋值"></a>变量及赋值</h3><p>python 是一个<strong>弱类型</strong>的语言，定义变量时不需要声明数据类型。变量的数据类型通过赋值时右侧的数据值类型决定。这叫<strong>隐式声明(implicit declaration)</strong></p>
<p><code>=</code>用于赋值变量，将<code>=</code>右侧的<strong>数据值</strong>或<strong>表达式结果</strong>赋值给左侧的变量名。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">myVar = <span class="string">&quot;abc&quot;</span>  <span class="comment"># 这时候myVar就是个字符串类型的变量</span></span><br><span class="line">myVar = <span class="number">123</span>    <span class="comment"># 这时候myVar就是个整数型的变量</span></span><br></pre></td></tr></table></figure>
<h3 id="检查变量类型、类型转换"><a href="#检查变量类型、类型转换" class="headerlink" title="检查变量类型、类型转换"></a>检查变量类型、类型转换</h3><p><code>type()</code>类用于检查某个变量值（或表达式）的类型</p>
<p><code>int()</code>类用于将数字字符串转换为整数型</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">myNum = <span class="built_in">input</span>()    <span class="comment"># 获取用户输入变量值（假定用户输入的一定是数字）</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(myNum)) <span class="comment"># 输出结果：&lt;class &#x27;str&#x27;&gt;</span></span><br><span class="line"><span class="comment"># str就是string的简写</span></span><br><span class="line"><span class="comment"># 也就是变量myNum的类型是字符串</span></span><br></pre></td></tr></table></figure>
<p>如果用户输入数值，且该数值需要进行数学运算，则需要转换类型</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="built_in">int</span>(myNum) + <span class="number">1</span>) <span class="comment"># 假设用户输入的值是 5，那么输出就会变为6</span></span><br></pre></td></tr></table></figure>
<p><strong>注意：</strong>但是用户如果输入的是非数值，则会报错<code>invalid literal for int() with base 10: &#39;&#39;</code></p>
<h3 id="字符串拼接"><a href="#字符串拼接" class="headerlink" title="字符串拼接"></a>字符串拼接</h3><p><code>+</code>号用来进行字符串与字符串拼接</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;Hello, &quot;</span> + <span class="string">&quot;world!&quot;</span>) <span class="comment"># 输出结果：Hello, world!</span></span><br></pre></td></tr></table></figure>
<p>但是字符串不能与非字符串进行拼接</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;Hello, &quot;</span> + <span class="number">123</span>) <span class="comment"># 这是错误写法</span></span><br></pre></td></tr></table></figure>
<p>上面会报错：<code>can only concatenate str (not &quot;int&quot;) to str</code>意思是：字符串只能与字符串（str）拼接而不是整数（int）</p>
<p><strong>解决方法：</strong></p>
<p>用<code>str()</code>转换为字符串类型</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;Hello, &quot;</span> + <span class="built_in">str</span>(<span class="number">123</span>)) <span class="comment"># 类型转换</span></span><br></pre></td></tr></table></figure>
<h2 id="第一课小结"><a href="#第一课小结" class="headerlink" title="第一课小结"></a>第一课小结</h2><p>用上面学到的知识点，可以写出一个基本的功能：</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">userName = <span class="built_in">input</span>(<span class="string">&quot;请输入您的名字：&quot;</span>)  <span class="comment"># 获取用户输入，并给予输入提示</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;您好，&quot;</span> + userName)          <span class="comment"># 打招呼</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;请输入您需要计算的值&quot;</span>)</span><br><span class="line">varA = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;初值：&quot;</span>))         <span class="comment"># 获取用户输入，并转换成整数</span></span><br><span class="line">varB = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;被加数：&quot;</span>))</span><br><span class="line">answer = varA + varB               <span class="comment"># 整数型 + 整数型 = 整数型</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;答案：&quot;</span> + <span class="built_in">str</span>(answer))       <span class="comment"># 因为answer是整数型，所以需要转换为字符串类型</span></span><br></pre></td></tr></table></figure>
<p>我们又学会了一个新的写法：<code>int(input())</code><br>当python遇到这个语句时，他会先执行<code>input()</code>，获取用户输入的数据，然后再执行<code>int()</code>转换input的返回值</p>
<p>当然，你也可以这么写：</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;答案：&quot;</span> + <span class="built_in">str</span>(varA + varB))</span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;答案：&quot;</span>, varA + varB)</span><br></pre></td></tr></table></figure>
<p><strong>原因：</strong>两个数据值相加得到的结果可以理解为表达式的返回值（这句有点绕），拆分来看就是：</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">str</span>(varA + varB) <span class="comment"># 假设：varA = 16, varB = 32</span></span><br><span class="line"><span class="built_in">str</span>(<span class="number">16</span> + <span class="number">32</span>)</span><br><span class="line"><span class="built_in">str</span>(<span class="number">48</span>)</span><br><span class="line"><span class="string">&quot;48&quot;</span> <span class="comment"># 最终结果</span></span><br></pre></td></tr></table></figure>
<p>所以由此可见，python是一门简单易学，且灵活的编程语言。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://kingsmai.github.io">小麦 Andrew Xiaomai</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://kingsmai.github.io/2021/10/09/%E7%AC%AC%201%20%E8%AF%BE/">https://kingsmai.github.io/2021/10/09/第 1 课/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kingsmai.github.io" target="_blank">麦甜怪圈 Xiaomai Circle</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://wallpaper.dog/small/5558009.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i>欢迎打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wallets/WechatPaySquare.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wallets/WechatPaySquare.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/wallets/AlipaySquare.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wallets/AlipaySquare.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/10/09/%E7%AC%94%E8%AE%B0/" title="C++ 笔记"><img class="cover" src="https://wallpaper.dog/small/5558009.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">C++ 笔记</div></div></a></div><div class="next-post pull-right"><a href="/2021/10/09/%E7%AC%AC%202%20%E8%AF%BE/" title="Python 第二课"><img class="cover" src="https://wallpaper.dog/small/5558031.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Python 第二课</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://avatars.githubusercontent.com/u/23566051?s=400&amp;u=cc8834557b9fe7ca3db3adc89606ae47503e603d&amp;v=4" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">小麦 Andrew Xiaomai</div><div class="author-info__description">一个喜欢肉鸽类游戏的程序员<br>分享日常生活，学习笔记，技术文章等。感兴趣想一起开发游戏的欢迎联系我哟！</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">37</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><a id="card-info-btn" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/kingsmai"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/kingsmai" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="iconfont icon-GitHub" style="color: #24292e;"></i></a><a class="social-icon" href="https://space.bilibili.com/670118055" rel="external nofollow noreferrer" target="_blank" title="BiliBili"><i class="iconfont icon-bilibili-fill" style="color: #ff6699;"></i></a><a class="social-icon" href="mailto:xsbugh@gmail.com" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="iconfont icon-email-fill" style="color: #ff3e30;"></i></a><a class="social-icon" href="https://www.zhihu.com/people/creeper0924" rel="external nofollow noreferrer" target="_blank" title="Zhihu"><i class="iconfont icon-zhihu" style="color: #1772F6;"></i></a><a class="social-icon" href="https://blog.csdn.net/Xsbugh" rel="external nofollow noreferrer" target="_blank" title="CSDN"><i class="iconfont icon-csdn" style="color: #FC4144;"></i></a><a class="social-icon" href="https://www.linkedin.com/in/%E5%B0%8F%E9%BA%A6-xiaomai-0a672124b/" rel="external nofollow noreferrer" target="_blank" title="LinkedIn"><i class="iconfont icon-linkedin" style="color: #0A66C2;"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS 链接"><i class="iconfont icon-Atom" style="color: #6EA3E5;"></i></a><a class="social-icon" href="/rss2.xml" target="_blank" title="RSS 链接"><i class="iconfont icon-dingyue" style="color: #ee802f;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="card-widget"><div class="item-headline"><i></i><span></span></div><div class="item-content"></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%AF%BE%E5%89%8D%E9%A1%BB%E7%9F%A5"><span class="toc-number">2.</span> <span class="toc-text">课前须知</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Python%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85"><span class="toc-number">2.1.</span> <span class="toc-text">Python环境安装</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Python-%E6%95%99%E5%AD%A6%E3%80%90%E7%AC%AC%E4%B8%80%E8%AF%BE%E3%80%91"><span class="toc-number">3.</span> <span class="toc-text">Python 教学【第一课】</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8CMD%E6%8C%87%E4%BB%A4"><span class="toc-number">3.1.</span> <span class="toc-text">常用CMD指令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Python-%E5%9F%BA%E7%A1%80%EF%BC%9AHello-World"><span class="toc-number">3.2.</span> <span class="toc-text">Python 基础：Hello, World!</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E9%87%8A"><span class="toc-number">3.2.1.</span> <span class="toc-text"># - 注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BE%93%E5%87%BAprint-%E5%9C%A8%E6%98%BE%E7%A4%BA%E5%99%A8%E4%B8%8A"><span class="toc-number">3.2.2.</span> <span class="toc-text">输出print() - 在显示器上</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BE%93%E5%85%A5input-%E7%94%A8%E6%88%B7%E4%BB%8E%E9%94%AE%E7%9B%98%E8%BE%93%E5%85%A5"><span class="toc-number">3.2.3.</span> <span class="toc-text">输入input() - 用户从键盘输入</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E5%8F%8A%E8%B5%8B%E5%80%BC"><span class="toc-number">3.2.4.</span> <span class="toc-text">变量及赋值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A3%80%E6%9F%A5%E5%8F%98%E9%87%8F%E7%B1%BB%E5%9E%8B%E3%80%81%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">3.2.5.</span> <span class="toc-text">检查变量类型、类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%BC%E6%8E%A5"><span class="toc-number">3.2.6.</span> <span class="toc-text">字符串拼接</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E8%AF%BE%E5%B0%8F%E7%BB%93"><span class="toc-number">3.3.</span> <span class="toc-text">第一课小结</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/12/20/ML-8-%E6%9E%84%E5%BB%BA%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E6%A8%A1%E5%9E%8B/" title="Sololearn 自学机器学习（8）构建逻辑回归模型"><img src="/img/covers/sklearn-build-linear-regression-model.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（8）构建逻辑回归模型"/></a><div class="content"><a class="title" href="/2023/12/20/ML-8-%E6%9E%84%E5%BB%BA%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E6%A8%A1%E5%9E%8B/" title="Sololearn 自学机器学习（8）构建逻辑回归模型">Sololearn 自学机器学习（8）构建逻辑回归模型</a><time datetime="2023-12-19T16:15:57.000Z" title="发表于 2023-12-20 00:15:57">2023-12-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/19/ML-7-%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E6%A8%A1%E5%9E%8B/" title="Sololearn 自学机器学习（7）线性回归模型"><img src="/img/covers/linear-regression-3.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（7）线性回归模型"/></a><div class="content"><a class="title" href="/2023/12/19/ML-7-%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E6%A8%A1%E5%9E%8B/" title="Sololearn 自学机器学习（7）线性回归模型">Sololearn 自学机器学习（7）线性回归模型</a><time datetime="2023-12-19T08:07:36.000Z" title="发表于 2023-12-19 16:07:36">2023-12-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/19/ML-6-%E5%88%86%E7%B1%BB-Classification/" title="Sololearn 自学机器学习（6）分类 Classification"><img src="/img/covers/classifications-3.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（6）分类 Classification"/></a><div class="content"><a class="title" href="/2023/12/19/ML-6-%E5%88%86%E7%B1%BB-Classification/" title="Sololearn 自学机器学习（6）分类 Classification">Sololearn 自学机器学习（6）分类 Classification</a><time datetime="2023-12-19T05:36:19.000Z" title="发表于 2023-12-19 13:36:19">2023-12-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/19/ML-5-Matplotlib-%E7%BB%98%E5%9B%BE%E5%9F%BA%E7%A1%80/" title="Sololearn 自学机器学习（5）Matplotlib 绘图基础"><img src="/img/covers/matplotlib-plot-basic-5.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（5）Matplotlib 绘图基础"/></a><div class="content"><a class="title" href="/2023/12/19/ML-5-Matplotlib-%E7%BB%98%E5%9B%BE%E5%9F%BA%E7%A1%80/" title="Sololearn 自学机器学习（5）Matplotlib 绘图基础">Sololearn 自学机器学习（5）Matplotlib 绘图基础</a><time datetime="2023-12-18T17:08:34.000Z" title="发表于 2023-12-19 01:08:34">2023-12-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/18/ML-4-Numpy-%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86/" title="Sololearn 自学机器学习（4）Numpy 数据处理"><img src="/img/covers/numpy-data-processing-2.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（4）Numpy 数据处理"/></a><div class="content"><a class="title" href="/2023/12/18/ML-4-Numpy-%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86/" title="Sololearn 自学机器学习（4）Numpy 数据处理">Sololearn 自学机器学习（4）Numpy 数据处理</a><time datetime="2023-12-18T13:26:11.000Z" title="发表于 2023-12-18 21:26:11">2023-12-18</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://wallpaper.dog/small/5558009.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2023 By 小麦 Andrew Xiaomai</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="https://icp.gov.moe/?keyword=20235160" rel="external nofollow noreferrer" target="_blank"><img class="icp-icon" src="/img/moe-icons/icon120.png">备案号：✮萌ICP备20235160号✮</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><canvas class="fireworks" mobile="true"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>